﻿
using log4net;
using MySql.Data.MySqlClient;
using server;
using server.Business;
using server.DAO;
using server.Model;
using server.Model.WebAdmin;
using server.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using PagedList;

namespace Restful
{
    //多线程
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ManagerImp : IManager
    {
        ILog Logger = log4net.LogManager.GetLogger("Restful.ManagerImp");

        public Admin AdminLogin(string userName, string password, string validateCode)
        {
            string sql = "select * from admin where username = @username and password = @password";
            List<Admin> admins = MySqlExecuteTools.GetObjectResult<Admin>(sql,
                new MySqlParameter[] { new MySqlParameter("@username", userName), new MySqlParameter("@password", password) });

            if(admins.Count>0)
            {
                return admins[0];
            }
            else
            {
                return null;
            }
        }

        public int ModityAdminPassword(string userID, string oldPassword, string repassword)
        {
            oldPassword = oldPassword.Trim();
            repassword = repassword.Trim();
            Admin admin  = MySqlExtendTools.GetEntityObjectByPrimaryKey<Admin>(userID,"admin");
            if(!admin.password.Equals(oldPassword))
            {
                return -1;
            }
            string  sql = "update admin set password = '" + repassword + "' where id = "+ userID;
            MySqlExecuteTools.AddOrUpdate(sql);
            return 0;

        }

        public UserReplay GetUserList(string limit, string page, string name)
        {
            string sql = "select * from user";
            List<UserItem> list = MySqlExecuteTools.GetObjectResult<UserItem>(sql);
            if(!string.IsNullOrEmpty(name))
            {
                list = list.Where(p => p.name.Contains(name)).ToList<UserItem>();
            }

            List<UserItem> resultList = list.ToPagedList(int.Parse(page), int.Parse(limit)).ToList<UserItem>();
            UserReplay userReplay = new UserReplay
            {
                data = resultList,
                count = list.Count.ToString(),
                msg = "ok"
            };
            return userReplay;
        }

        public GrounpReplay GetGameList(string limit, string page, string name)
        {
            string sql = "select * from grounp";
            List<Grounp> list = MySqlExecuteTools.GetObjectResult<Grounp>(sql);
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(p => p.name.Contains(name)).ToList<Grounp>();
            }

            List<Grounp> resultList = list.ToPagedList(int.Parse(page), int.Parse(limit)).ToList<Grounp>();


            GrounpReplay grounpReplay = new GrounpReplay
            {
                data = resultList,
                msg = "ok",
                count = list.Count.ToString()
            };

            return grounpReplay;
        }

        public CodeReplay GetActiveCodeList(string limit, string page, string code)
        {
            string sql = "select * from code";
            List<CodeModel> list = MySqlExecuteTools.GetObjectResult<CodeModel>(sql);

            if (!string.IsNullOrEmpty(code))
            {
                list = list.Where(p => p.code.Contains(code)).ToList<CodeModel>();
            }

            List<CodeModel> resultList = list.ToPagedList(int.Parse(page), int.Parse(limit)).ToList<CodeModel>();


            CodeReplay codeReplay = new CodeReplay
            {
                data = resultList,
                msg = "ok",
                count = list.Count.ToString()
            };

            return codeReplay;
        }

        public int GetRandomActiveCode()
        {
            int result = -1;
            while(true)
            {
                Random random = new Random();

                result =  random.Next();

                string sql = "select * from code where code = "+ result;

                int count = MySqlExecuteTools.GetCountResult(sql);
                if(count==0)
                {
                    break;
                }

            }
            return result;
        }


        public string Test(string stu)
        {
            //throw new NotImplementedException();
            Console.WriteLine(stu);
            return "";
        }

        public int AddCode(string code, string codeType, string note, string expiretime)
        {
            if(expiretime.Equals("0"))
            {
                expiretime = DateTime.Now.ToString("yyyy-MM-dd");
            }

            string sql = "insert into code(code,note,expiretime,codeType) " +
                  "values('" + code + "','" + note + "','" + expiretime + "','" + codeType + "')";
            MySqlExecuteTools.AddOrUpdate(sql);
            return 0;

        }

        public int ModifyCode(string id, string note, string codeType, string expiretime)
        {
            if (expiretime.Equals("0"))
            {
                expiretime = DateTime.Now.ToString("yyyy-MM-dd");
            }

            string sql =  "update code set note = '" + note + "', expiretime = '" + expiretime + "', codeType = '" + codeType + "' where id = " +id;

            MySqlExecuteTools.AddOrUpdate(sql);
            return 0;
        }

        public int DeleteCode(string id)
        {
            MySqlExtendTools.DeleteByPrimaryKey(int.Parse( id), "code");
            return 0;
        }

        public CodeModel SearchCodeById(string id)
        {
            // throw new NotImplementedException();
           return  MySqlExtendTools.GetEntityObjectByPrimaryKey<CodeModel>(id, "code");

        }

        public CodeReplay AdminLogOut()
        {
            CodeReplay codeReplay = new CodeReplay();
            return codeReplay;
        }
    }
}
